package com.beifu.manager.service.washfeedback.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beifu.manager.api.domain.feedback.FeedBackDetailResponse;
import com.beifu.manager.api.domain.feedback.FeedBackInfo;
import com.beifu.manager.api.domain.feedback.FeedBackParam;
import com.beifu.manager.dao.AssetOutMapper;
import com.beifu.manager.dao.ImgMapper;
import com.beifu.manager.dao.TrainInfoMapper;
import com.beifu.manager.dao.UserInfoMapper;
import com.beifu.manager.dao.WashFeedbackMapper;
import com.beifu.manager.dao.WashFeedbackRfidsMapper;
import com.beifu.manager.model.AssetOut;
import com.beifu.manager.model.Img;
import com.beifu.manager.model.TrainInfo;
import com.beifu.manager.model.UserInfo;
import com.beifu.manager.model.WashFeedback;
import com.beifu.manager.model.WashFeedbackExample;
import com.beifu.manager.model.WashFeedbackExample.Criteria;
import com.beifu.manager.model.WashFeedbackRfids;
import com.beifu.manager.service.washfeedback.WashFeedBackRfidsService;
import com.beifu.manager.service.washfeedback.WashFeedBackService;
import com.mds.manager.utils.PageUtils;

@Service
public class WashFeedBackServiceImpl implements WashFeedBackService {
	@Autowired
	private WashFeedbackMapper washFeedBackMapper;
	@Autowired
	private UserInfoMapper userInfoMapper;
	@Autowired
	private TrainInfoMapper trainInfoMapper;
	@Autowired
	private WashFeedBackRfidsService washFeedBackRfidsService;
	@Autowired
	private WashFeedbackRfidsMapper washFeedBackRfidsMapper;
	@Autowired
	private ImgMapper imgMapper;
	@Autowired
	private AssetOutMapper assetOutMapper;
	
	@Override
	public PageUtils getWashFeedBackList(PageUtils page, String parameter) {
		
		//获取分页查询的数据
		WashFeedbackExample example = new WashFeedbackExample();
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(parameter)){
			
			criteria.andAssetRfidCodeEqualTo(parameter);
		}
		//执行查询
		List<WashFeedback> list = washFeedBackMapper.getWashFeedPage((page.getPage()-1)*page.getLimit(),page.getLimit(), example);
		for (WashFeedback washFeedback : list) {
			Integer createUserId = washFeedback.getCreateUserId();
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(createUserId);
			if(null != userInfo){
				washFeedback.setCreateUserName(userInfo.getUserName());
			}
		}
		
        int count = washFeedBackMapper.countByExample(example);
        page.setList(list);
        page.setCountNum(count);
		return page;
	}

	@Override
	public WashFeedback getWashFeedDetail(String id) {
		if(StringUtils.isEmpty(id)){
			return null;
		}
		WashFeedback washFeedback = washFeedBackMapper.selectByPrimaryKey(Integer.parseInt(id));
		UserInfo userInfo = userInfoMapper.selectByPrimaryKey(washFeedback.getCreateUserId());
		TrainInfo trainInfo = null;
		if(null != userInfo){
			washFeedback.setCreateUserName(userInfo.getUserName());
			trainInfo = trainInfoMapper.selectByPrimaryKey(userInfo.getOrganizeId());
		}
		if(null != trainInfo){
			//FIXME 车辆编号
			washFeedback.setTrainId(trainInfo.getTrainNo());
//			washFeedback.setTrainId(String.valueOf(trainInfo.getId()));
//			washFeedback.setTrainId(trainInfo.getId());
			washFeedback.setTrainNumber(trainInfo.getTrainNumber());
		}
		String images = washFeedback.getFeedbackImage();//多个图片id
		
		//获取洗涤反馈照片
		List<String> imgList = getImgs(images);
		
		washFeedback.setImgList(imgList);
		return washFeedback;
	}
/**
 * 更据订单id获取照片
 * @param images
 * @return
 */
	private List<String> getImgs(String images) {
		List<String> imgList = new ArrayList<String>();
		
		if(StringUtils.isNotEmpty(images)){
			String[] imgIds = images.split(",");
			for (String imgId : imgIds) {
				Img img = imgMapper.selectByPrimaryKey(Long.parseLong(imgId));
				if(null != img){
					imgList.add(new String(img.getImgbase64()));
				}
			}
		}
		
		return imgList;
	}

	@Override
	public List<FeedBackInfo> getFeedBackList(String pageIndex, String pageNumber,String userId) {
        int pagerCur = Integer.parseInt(pageIndex);
        int pageSize = Integer.parseInt(pageNumber);
        List<FeedBackInfo> list = new ArrayList<FeedBackInfo>();
		WashFeedbackExample example = new WashFeedbackExample();
		example.createCriteria().andCreateUserIdEqualTo(Integer.valueOf(userId));
		List<WashFeedback> washFeedPage = washFeedBackMapper.getWashFeedPage((pagerCur-1)*pageSize, pageSize, example);
		if(null != washFeedPage && washFeedPage.size() > 0){
			for (WashFeedback washFeedback : washFeedPage) {
				FeedBackInfo backInfo = new FeedBackInfo();
				backInfo.setWashBackId(washFeedback.getId()+"");
				backInfo.setStatus(washFeedback.getFeedbackStatus());
				
				list.add(backInfo);
			}
		}
		
		return list;
	}

	@Override
	public FeedBackDetailResponse getFeedBackDetail(String id) {
		FeedBackDetailResponse scrap = new FeedBackDetailResponse();//返回前端数据
		
		WashFeedback washFeedback = washFeedBackMapper.selectByPrimaryKey(Integer.parseInt(id));
		if(null == washFeedback){
			return null;
		}
		UserInfo userInfo = userInfoMapper.selectByPrimaryKey(washFeedback.getCreateUserId());
		TrainInfo trainInfo = null;
		if(null != userInfo){
			scrap.setApplyName(userInfo.getUserName());
			trainInfo = trainInfoMapper.selectByPrimaryKey(userInfo.getOrganizeId());
		}
		if(null != trainInfo){
//			scrap.setTrainID(trainInfo.getId()+"");
			scrap.setTrainID(trainInfo.getTrainNo()+"");
		}
		scrap.setTrainTime(washFeedback.getTrainNumber());
		String createTime = new SimpleDateFormat("yyyy-MM-dd").format(washFeedback.getCreateTime());
        scrap.setBackDate(createTime);
        scrap.setApplyDate(createTime);
        scrap.setDesc(washFeedback.getFeedbackContent());
        
        //获取反馈照片
        String feedbackImages = washFeedback.getFeedbackImage();
        
        //用于返回前端的图片集合
        List<String> imgs = getImgs(feedbackImages);
        
        scrap.setImgs(imgs);
       
		List<FeedBackParam> prodList = washFeedBackRfidsService.getWashFeedBackRfids(washFeedback.getId());

	    scrap.setScrapList(prodList);
		return scrap;
	}

	@Override
	public void saveWashFeedBack(WashFeedback washFeedBack, List<FeedBackParam> scrapList) {
		//将数据插入到wash_feedback表中，同时返回主键
		List<String> imgList = washFeedBack.getImgList();
		String ids = "";
		if(null != imgList && imgList.size() > 0){
			for (String imgBase64 : imgList) {
				Img img = new Img();
				img.setImgbase64(imgBase64.getBytes());
				imgMapper.insertAndGetId(img);//将图片放入数据库并返回主键
				
				ids += img.getId() + ",";
			}
			ids = ids.substring(0, ids.length()-1);
		}
		washFeedBack.setFeedbackImage(ids);
		
		//获取洗涤厂id，通过洗涤单号获取源组织id
		String washId = washFeedBack.getAssetRfidCode();//洗涤单号
		if(StringUtils.isNotEmpty(washId)){
			AssetOut assetOut = assetOutMapper.selectByPrimaryKey(Integer.parseInt(washId));
			if(null != assetOut){
				//源组织id
				washFeedBack.setWashFactoryId(assetOut.getSrcOrganizeId());
			}
		}
		
		
		washFeedBackMapper.insertSelective(washFeedBack);
		Integer washFeedbackId = washFeedBack.getId();
		
		//将数据插入到wash_feedback_rfids
		if(null != scrapList && scrapList.size() > 0){
			for (FeedBackParam feedBackParam : scrapList) {
				String rfId = feedBackParam.getRFID();
				WashFeedbackRfids washFeedbackRfids = new WashFeedbackRfids();
				washFeedbackRfids.setAssetRfidCode(rfId);
				washFeedbackRfids.setWashFeedbackId(washFeedbackId);
				
				washFeedBackRfidsMapper.insertSelective(washFeedbackRfids);
			}
		}
		
	}
	
}
